package com.yanqu.road.server.gameplayer.module.activity.entername;

import com.yanqu.road.entity.activity.entername.ChannelFightMatchRule;
import com.yanqu.road.entity.activity.entername.ChannelFightZone;
import com.yanqu.road.entity.activity.entername.EnterNameUtils;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityConfig;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;
import java.util.Random;

public class EnterNameActivityModule extends GeneralModule {

    String ip_cityName = "";

    public EnterNameActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return false;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        return false;
    }

    @Override
    public void afterLogin() {
        syncConfig();
    }

    public void syncConfig() {
        Random random = new Random();

        EnterNameProto.CrossEnterNameSyncMsg.Builder crossSync = EnterNameProto.CrossEnterNameSyncMsg.newBuilder();

        EnterNameProto.ChannelEnterNameConfigSyncMsg.Builder sync = EnterNameProto.ChannelEnterNameConfigSyncMsg.newBuilder();
        for (EnterNameActivityConfig activityConfig : EnterNameActivityMgr.getAllInTimeConfig()) {

            //系统解锁
//            if(!EnterNameActivityMgr.isActivitySystemOpen(player, activityConfig.getActivityInfo())){
//                continue;
//            }

            EnterNameProto.ChannelEnterNameConfigListMsg.Builder builder = EnterNameProto.ChannelEnterNameConfigListMsg.newBuilder();
            builder.setActivityId(activityConfig.getActivityInfo().getActivityId());
            for (ChannelFightMatchRule matchRule : activityConfig.getMatchRuleList()) {
                EnterNameProto.ChannelFightMatchRuleMsg.Builder ruleBuilder = EnterNameProto.ChannelFightMatchRuleMsg.newBuilder();
                ruleBuilder.setRuleId(matchRule.getGroupId());
                ruleBuilder.setRankRange(matchRule.getRankRange());
                builder.addRule(ruleBuilder);
            }

            for (ChannelFightZone fightZone : activityConfig.getFightZoneList()) {
                EnterNameProto.ChannelFightZoneMsg.Builder zoneBuilder = EnterNameProto.ChannelFightZoneMsg.newBuilder();
                zoneBuilder.setAreaId(fightZone.getAreaId());
                zoneBuilder.setName(ServerLanguageMgr.getContent(fightZone.getName(), player.getUserInfo().getLanguage()));
                zoneBuilder.setAreaMember(ServerLanguageMgr.getContent(fightZone.getAreaMember(), player.getUserInfo().getLanguage()));
                builder.addZone(zoneBuilder);
            }

            //推荐区域
            int areaCode = EnterNameUtils.getAreaCodeByName(player.getUserInfo().getIpLocation());
            int zoneId = activityConfig.getAreaIdByMemberCode(areaCode, random);
            builder.setZoneId(zoneId);

            sync.addConfig(builder);

            //系统解锁
            if(!EnterNameActivityMgr.isActivitySystemOpen(player, activityConfig.getActivityInfo())){
                continue;
            }

            crossSync.addActivityId(activityConfig.getActivityInfo().getActivityId());
            if(ActivityMgr.activityInCanRewardPeriod(activityConfig.getActivityInfo())) {
                crossSync.addRewardActivityId(activityConfig.getActivityInfo().getActivityId());
            }
        }
        player.sendPacket(Protocol.U_ENTER_NAME_CONFIG_SYNC, sync);

        if(crossSync.getActivityIdCount() > 0) {
            crossSync.setKey(EnterNameActivityMgr.buildKeyMsg(player.getUserId()));
            player.sendPacket(Cross3Protocol.C3_ENTER_NAME_LOGIN_SYNC, crossSync);
            //EnterNameActivityMgr.getLogger().info("enter name C3_ENTER_NAME_LOGIN_SYNC req, {}", StringUtils.listToString(crossSync.getActivityIdList(), ","));
        }

        //获取ip定位城市
        long serverId = ConfigHelper.getLong("serverId");
        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
        int channelId = 0;
        if(serverInfo != null){
            channelId = serverInfo.getChannelId();
        }

        boolean hasActivity = false;
        for (Map.Entry<Integer, EnterNameActivityConfig> configEntry : EnterNameActivityMgr.getActivityConfigMap().entrySet()) {
            EnterNameActivityConfig config = configEntry.getValue();
            if(ActivityMgr.activityInShowTime(config.getActivityInfo())){
                hasActivity = true;
                break;
            }
        }
        if(hasActivity){
            //仅大陆渠道需要
            if(eChannelType.isMainLandChannel(channelId)){
                if(StringUtils.isNullOrEmpty(ip_cityName)){
                    String ip = player.getIp();
                    if(!StringUtils.isNullOrEmpty(ip)){
                        GameServer.getSqlCommandExecutor().execute(() -> {
                            String hLocation = "未知";
                            try {
                                hLocation = HttpHelper.doGet("https://login-cno.ngty556.com/Web/ip/cityName?ip=" + ip, "UTF-8");
                            } catch (Exception e) {

                            }
                            if(hLocation != null) {
                                ip_cityName = hLocation;
                            }
                        });
                    }
                }
            }
        }
    }

    public String getIp_cityName() {
        return ip_cityName;
    }

}
